﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace CRM_Solution_Merger
{
    public partial class MergeSolutionPopUp : Form
    {
        private List<CRMSolution> solutionList;
        public MergeSolutionPopUp()
        {
            InitializeComponent();
        }

        private void MergeSolutionPopUp_Load(object sender, EventArgs e)
        {
            solutionList = ((CRMSolutionMerger)this.Owner).CRMSolutionList;
            ddlExistingSolution.DataSource = solutionList;
            ddlExistingSolution.ValueMember = "SolutionId";
            ddlExistingSolution.DisplayMember = "Name";

            List<KeyValuePair<Guid, string>> PublisherList = ((CRMSolutionMerger)this.Owner).PublisherList;
            ddlPublisher.DataSource = PublisherList;
            ddlPublisher.ValueMember = "Key";
            ddlPublisher.DisplayMember = "Value";
        }

        public CRMSolution GetSolutionName()
        {
            //KeyValuePair<Guid, string> solutionName = new KeyValuePair<Guid, string>();

            CRMSolution solutionName = new CRMSolution();

            if (radioBtnNewSolution.Checked)
            {
                solutionName.SolutionId = Guid.Empty;
                solutionName.Name = txtNewSolutionName.Text;
            }
            else if (radioBtnMergeSolution.Checked)
            {
                solutionName.SolutionId = new Guid(ddlExistingSolution.SelectedValue.ToString());
                solutionName.Name = ddlExistingSolution.Text;
                solutionName.PublisherId = this.solutionList.Where(a => a.SolutionId == solutionName.SolutionId).Select(a => a.PublisherId).FirstOrDefault();
                solutionName.UniqueName = this.solutionList.Where(a => a.SolutionId == solutionName.SolutionId).Select(a => a.UniqueName).FirstOrDefault();
                solutionName.Version = this.solutionList.Where(a => a.SolutionId == solutionName.SolutionId).Select(a => a.Version).FirstOrDefault();
            }
            return solutionName;
        }

        public KeyValuePair<Guid, string> GetSelectedPublisher()
        {
            return new KeyValuePair<Guid, string>(new Guid(ddlPublisher.SelectedValue.ToString()), ddlPublisher.Text);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (radioBtnNewSolution.Checked)
            {
                string solUniqueName = txtNewSolutionName.Text.Replace(" ", "");

                if (this.solutionList.Any(a => string.Compare(a.UniqueName, solUniqueName, true) == 0))
                {
                    MessageBox.Show(string.Format("Solution Name: {0} is already exists, select another name", txtNewSolutionName.Text));
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void radioBtnNewSolution_CheckedChanged(object sender, EventArgs e)
        {
            if (radioBtnNewSolution.Checked)
            {
                txtNewSolutionName.Visible = true;
                lblSolText.Visible = true;
                ddlExistingSolution.Visible = false;
                lblSolDDL.Visible = false;
            }
            else
            {
                txtNewSolutionName.Visible = false;
                lblSolText.Visible = false;
                ddlExistingSolution.Visible = true;
                lblSolDDL.Visible = true;
            }
        }

        private void radioBtnMergeSolution_CheckedChanged(object sender, EventArgs e)
        {
            if (radioBtnMergeSolution.Checked)
            {
                txtNewSolutionName.Visible = false;
                lblSolText.Visible = false;
                ddlExistingSolution.Visible = true;
                lblSolDDL.Visible = true;
            }
            else
            {
                txtNewSolutionName.Visible = true;
                lblSolText.Visible = true;
                ddlExistingSolution.Visible = false;
                lblSolDDL.Visible = false;
            }
        }

        private bool CheckVersionNumber(string versionNumber)
        {
            string pattern =@"\d+\.\d+\.\d+\.\d+";
            var regEx = new Regex(pattern, RegexOptions.IgnoreCase);
            var versionMatch = regEx.Match(versionNumber);
            return versionMatch.Success;
        }
    }
}
